local bishuo = fk.CreateSkill {
  name = "bishuo",
}

Fk:loadTranslationTable {
  ["bishuo"] = "闭锁",
  ["$bishuo1"] = "加税",
  ["#bishuo-fordamage"] = "闭锁：你即将受到<b>%arg</b>点伤害，你可以令一名其他角色选择是否受到相同的效果，若其选否则你防止此伤害",
  ["#bishuo-forlost"] = "闭锁：你即将失去<b>%arg</b>点体力，你可以令一名其他角色选择是否受到相同的效果，若其选否则你防止此次体力流失",
  ["#bishuo-formaxhp"] = "闭锁：你即将减少<b>%arg</b>点体力上限，你可以令一名其他角色选择是否受到相同的效果，若其选否则你防止此次减少",
  ["#bishuo-forskill"] = "闭锁：你失去了<b>【%arg】</b>技能，你可以令一名其他角色选择是否失去一个技能，若其选否则你重新获得此技能",
  ["#bishuo"] = "闭锁",
  ["#bishuo-dama"] = "闭锁：请你选择是否受到 <b>%arg</b> 点伤害，若选否，则防止 <b>%src</b> 受到的伤害",
  ["#bishuo-lose"] = "闭锁：请你选择是否失去 <b>%arg</b> 点体力，若选否，则防止 <b>%src</b> 失去的体力",
  ["#bishuo-maxhp"] = "闭锁：请你选择是否减 <b>%arg</b> 点体力上限，若选否，则防止 <b>%src</b> 减少的体力上限",
  ["#bishuo-loseskill"] = "闭锁：请你选择是否失去一个技能，若选否，则 <b>%src</b> 将重新获得技能<b>【%arg】</b>",
  [":bishuo"] = "你受到伤害时，失去体力时，减少体力上限时，失去技能时，你可令一名其他角色选择是否受到相同的效果，若其选择否则你防止之。",
  ["#bishuo-general"] = "闭锁：你选择主将或副将",
  ["#bishuo_copy"] = "闭锁：请选择要失去的技能",
}

bishuo:addEffect(fk.DamageInflicted, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(bishuo.name) and target == player
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      prompt = "#bishuo-fordamage:::" .. data.damage,
      skill_name = bishuo.name,
      cancelable = true,
    })
    if #targets > 0 then
      event:setCostData(self, { target = targets[1] })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).target
    if player.room:askToSkillInvoke(to, { skill_name = bishuo.name, prompt = "#bishuo-dama:"..player.id.."::" .. data.damage }) then
      room:damage({
        from = data.from,
        to = to,
        damage = data.damage,
        skillName = bishuo.name
      })
    else
      data:preventDamage()
    end
  end
})

bishuo:addEffect(fk.PreHpLost, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(bishuo.name) and target == player
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      prompt = "#bishuo-forlost:::" .. data.num,
      skill_name = bishuo.name,
      cancelable = true,
    })
    if #targets > 0 then
      event:setCostData(self, { target = targets[1] })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).target
    if player.room:askToSkillInvoke(to, { skill_name = bishuo.name, prompt = "#bishuo-lose:"..player.id.."::" .. data.num }) then
      room:loseHp(to, data.num, bishuo.name)
    else
      data:preventHpLost()
    end
  end
})

bishuo:addEffect(fk.BeforeMaxHpChanged, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(bishuo.name) and target == player and data.num < 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      prompt = "#bishuo-formaxhp:::" .. -data.num,
      skill_name = bishuo.name,
      cancelable = true,
    })
    if #targets > 0 then
      event:setCostData(self, { target = targets[1] })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).target
    if player.room:askToSkillInvoke(to, { skill_name = bishuo.name, prompt = "#bishuo-maxhp:"..player.id.."::" .. -data.num }) then
      room:changeMaxHp(to, data.num)
    else
      data:preventMaxHpChange()
    end
  end
})

bishuo:addLoseEffect(function (self, player, is_death)
  local room = player.room
    local targets = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      prompt = "#bishuo-forskill:::" .. Fk:translate(self.name),
      skill_name = bishuo.name,
      cancelable = true,
    })
    if #targets > 0 then
      local to = targets[1]
      if player.room:askToSkillInvoke(to, { skill_name = bishuo.name, prompt = "#bishuo-loseskill:"..player.id.."::" .. Fk:translate(self.name) }) then
        local skills = {}
        for _, s in ipairs(to:getSkillNameList()) do
          local skill = Fk.skills[s]
          if not skill.attached_equip and not s:startsWith("#") then
            table.insert(skills, s)
          end
        end
        if #skills > 0 then
          local skill = room:askToChoice(to, {
            choices = skills,
            skill_name = bishuo.name,
            detailed = true,
            cancelable = false,
            prompt = "#bishuo_copy",
          })
          if skill then
            room:handleAddLoseSkills(to, "-" .. skill)
          end
        end
      else
        room:handleAddLoseSkills(player, "bishuo", nil, false, true)
      end
    end
end)

bishuo:addEffect(fk.EventLoseSkill, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(bishuo.name) and target == player
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      prompt = "#bishuo-forskill:::" .. Fk:translate(data.skill.name),
      skill_name = bishuo.name,
      cancelable = true,
    })
    if #targets > 0 then
      event:setCostData(self, { target = targets[1] })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).target --[[@as ServerPlayer]]
    if player.room:askToSkillInvoke(to, { skill_name = bishuo.name, prompt = "#bishuo-loseskill:"..player.id.."::" .. Fk:translate(data.skill.name) }) then
      local skills = {}
      for _, s in ipairs(to:getSkillNameList()) do
        local skill = Fk.skills[s]
        if not skill.attached_equip and not s:startsWith("#") then
          table.insert(skills, s)
        end
      end
      if #skills > 0 then
        local skill = room:askToChoice(to, {
          choices = skills,
          skill_name = bishuo.name,
          detailed = true,
          cancelable = false,
          prompt = "#bishuo_copy",
        })
        if skill then
          room:handleAddLoseSkills(to, "-" .. skill)
        end
      end
    else
      room:handleAddLoseSkills(player, data.skill.name, nil, false, true)
    end
  end
})


return bishuo
